﻿/*LEEME !!!!
 * 
 * PARA CONFIGURAR ESTA CLASE, HACER DOBLE CLICK EN LA IMAGEN DE INICIO DE SESION.
 * LA CLAVE DE ACCESO ES "ILAB"
 * Y PODRAS CONFIGURAR LA CONEXION
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//conexion a mysql
using System.Xml;
using MySql.Data.MySqlClient;
using System.Data;
using System.Xml.Linq;

namespace CapaNegocio
{
    public class clsConexion
    {
        //private string servidor = "localhost";
        //private string usuario = "root";
        //private string contraseña = "";
        //private string basedato = "ilab";
        //cadenaconexion = "SERVER=" + servidor + ";" + "DATABASE=" + basedato + ";" + "UID=" + usuario + ";" + "PASSWORD=" + contraseña + ";";

        public string servidor      { get; set; }
        public string usuario       { get; set; }
        public string contraseña    { get; set; }
        public string basedato      { get; set; }

        public MySqlConnection conexion;
        private MySqlCommand command;
        private MySqlDataReader Reader;

        private static String SERVER;
        private static String DATABASE;
        private static String USER;
        private static String PASSWORD;
        //public string cadenaconexionx { get; set; } //= "SERVER="+servidor+"; DATABASE=ilab6; UID=root; PASSWORD=ltry";

        //string cadenaconexion = "SERVER=localhost; DATABASE=ilab6; UID=ilab; PASSWORD=ilab";
        //string cadenaconexion = "SERVER=localhost; DATABASE=ilab6; UID=root; PASSWORD=ltry";

        public clsExamen clsExamen
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }


        public void LeerYModificarXML()
        {
            XmlTextWriter writer;
            writer = new XmlTextWriter("rutaconexion.xml", Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("cadena");
            writer.WriteStartElement("conexion");
            writer.WriteElementString("server", this.servidor);
            writer.WriteElementString("database", this.basedato);
            writer.WriteElementString("user", this.usuario);
            writer.WriteElementString("password", this.contraseña);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        public void mtdLeerConexion()
        {

            //CrearDocumentoXML();
            XmlDocument myXmlDocument = new XmlDocument();
            //LeerYModificarXML();            
            myXmlDocument.Load("rutaconexion.xml");
            XmlNodeList cadena = myXmlDocument.GetElementsByTagName("cadena");
            XmlNodeList lista = ((XmlElement)cadena[0]).GetElementsByTagName("conexion");

            foreach (XmlElement nodo in lista)
            {

                XmlNodeList server      = nodo.GetElementsByTagName("server");
                XmlNodeList database    = nodo.GetElementsByTagName("database");
                XmlNodeList user        = nodo.GetElementsByTagName("user");
                XmlNodeList password    = nodo.GetElementsByTagName("password");

                //server = nodo.SetAttribute("dir","HOLA MUNDO!");
                //System.Windows.Forms.MessageBox.Show(server[0].InnerText);    

                this.servidor   = server[0].InnerText;
                this.basedato   = database[0].InnerText;
                this.usuario    = user[0].InnerText;
                this.contraseña = password[0].InnerText;

                SERVER      = this.servidor;
                DATABASE    = this.basedato;
                USER        = this.usuario;
                PASSWORD    = this.contraseña;

                //System.Windows.Forms.MessageBox.Show(this.servidor);
                //System.Windows.Forms.MessageBox.Show(this.basedato);
                //System.Windows.Forms.MessageBox.Show(this.usuario);    
                //System.Windows.Forms.MessageBox.Show(this.contraseña);        

            }

        }


        //Abrir conexion a la base de datos
        public bool mtdConectar()
        {

            try
            {
                mtdLeerConexion();
                string cadenaconexionx = "SERVER=" + SERVER + "; DATABASE=" + DATABASE + "; UID=" + USER + "; PASSWORD=" + PASSWORD;
                //string cadenaconexion = "SERVER=localhost; DATABASE=ilab6; UID=root; PASSWORD=ltry";
                conexion = new MySqlConnection(cadenaconexionx);
                conexion.Open();
                //probando conexion
                //System.Windows.Forms.MessageBox.Show("Conexion Establecida");
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
                //System.Windows.Forms.MessageBox.Show("Conexion NO Establecida");

            }
        }

        //Cerrar conexion a la base de datos
        public bool mtdDesconectar()
        {

            //String chain = null;
            //chain = this.cadenaconexionx;

            try
            {
                //string cadenaconexion = "SERVER=" + this.servidor + "; DATABASE=ilab6; UID=root; PASSWORD=ltry";
                mtdLeerConexion();
                string cadenaconexionx = "SERVER=" + SERVER + "; DATABASE=" + DATABASE + "; UID=" + USER + "; PASSWORD=" + PASSWORD;                
                conexion = new MySqlConnection(cadenaconexionx);
                conexion.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }

        public clsUsuario clsUsuario
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public clsBoleta clsBoleta
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public clsInventario clsInventario
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public clsPaciente clsPaciente
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }
    }
}